#include "../games/globale.h" // Fonction et Headers globaux
#include "../multimedia/cls_mouse.h" // Classe des gestion du curseur de la souris

/* ----------------------------------------------------
			Constructeur
  ----------------------------------------------------- */
cls_Mouse::cls_Mouse()
{
    int i;

    debugTrace("Constructeur cls_Mouse");
    this->n_CurrentCursorType = -1;
    this->n_CurrentActionCursor = MOUSE_CURSOR_INFO;
    this->p_sdlS_CurrentMouse = NULL;


    for(i=0;i<NB_MOUSE_CURSOR_ARROW;i++)
    {
        this->p_sdlS_Mouse[i] = NULL;
    }

}

/* ----------------------------------------------------
			Destructeur
  ----------------------------------------------------- */
cls_Mouse::~cls_Mouse()
{
    debugTrace("Destructeur cls_Mouse");
}


/* ----------------------------------------------------
			Visibilité du curseur
  ----------------------------------------------------- */
void cls_Mouse::set_CursorDisplay(bool b_isVisible)
{
    // On enleve le curseur de la souris
    SDL_ShowCursor(b_isVisible);
}

/* ----------------------------------------------------
			On charge les différentes images du curseur
  ----------------------------------------------------- */
void cls_Mouse::fct_InitMouse()
{
    // Chargement des images du curseurs
    this->p_sdlS_Mouse[MOUSE_CURSOR_ARROW] = load_image(join_pathFile(PATH_DATA_IMG_MOUSE, "arrow.png"));
    this->p_sdlS_Mouse[MOUSE_CURSOR_MOVE] = load_image(join_pathFile(PATH_DATA_IMG_MOUSE, "action_walk.png"));
    this->p_sdlS_Mouse[MOUSE_CURSOR_INFO] = load_image(join_pathFile(PATH_DATA_IMG_MOUSE, "action_watch.png"));
    this->p_sdlS_Mouse[MOUSE_CURSOR_OPEN] = load_image(join_pathFile(PATH_DATA_IMG_MOUSE, "action_open.png"));
    this->p_sdlS_Mouse[MOUSE_CURSOR_CLOSE] = load_image(join_pathFile(PATH_DATA_IMG_MOUSE, "action_close.png"));
    this->p_sdlS_Mouse[MOUSE_CURSOR_TAKE] = load_image(join_pathFile(PATH_DATA_IMG_MOUSE, "action_take.png"));
    this->p_sdlS_Mouse[MOUSE_CURSOR_USE] = load_image(join_pathFile(PATH_DATA_IMG_MOUSE, "action_use.png"));
    this->p_sdlS_Mouse[MOUSE_CURSOR_TALK] = load_image(join_pathFile(PATH_DATA_IMG_MOUSE, "action_talk.png"));

    // On initialise la valeur par defaut
    // BUG JODA !!!
    //this->p_sdlS_CurrentMouse = this->p_sdlS_Mouse[MOUSE_CURSOR_ARROW];
}


/* ----------------------------------------------------
			définition du pointeur de la souris
  ----------------------------------------------------- */
void cls_Mouse::fct_SetMouseCursor(int n_CursorType)
{

    //static int current_CursorType;
    if(this->n_CurrentCursorType != n_CursorType)
    {
        /*
        this->p_sdlS_CurrentMouse = this->p_sdlS_Mouse[n_CursorType];
        */
        //this->p_sdlS_CurrentMouse = load_image(join_pathFile(PATH_DATA_IMG_MOUSE, "deplacement.png"));
        //this->p_sdlS_CurrentMouse = this->p_sdlS_Mouse[MOUSE_CURSOR_ARROW];
/*
    char toto[256];
    sprintf(toto, "Curseur : %d", n_CursorType);
    debugTrace(toto);
*/
        this->p_sdlS_CurrentMouse = this->p_sdlS_Mouse[n_CursorType];

/*
        switch(n_CursorType)
        {
            case MOUSE_CURSOR_ARROW:
                this->p_sdlS_CurrentMouse = load_image(join_pathFile(PATH_DATA_IMG_MOUSE, "arrow.png"));
                break;
            case MOUSE_CURSOR_MOVE:
                this->p_sdlS_CurrentMouse = load_image(join_pathFile(PATH_DATA_IMG_MOUSE, "deplacement.png"));
                break;
            case MOUSE_CURSOR_INFO:
                this->p_sdlS_CurrentMouse = load_image(join_pathFile(PATH_DATA_IMG_MOUSE, "loupe.png"));
                break;
        }
*/

        this->n_CurrentCursorType = n_CursorType;
    }

}


/* ----------------------------------------------------
    Permet d'obtenir le curseur d'action courant
  ----------------------------------------------------- */
int cls_Mouse::fct_GetCurrentActionCursor()
{
    return n_CurrentActionCursor;
}


/* ----------------------------------------------------
    Permet d'obtenir le curseur suivant dans la liste
        #define NB_MOUSE_CURSOR_ARROW   8
        #define MOUSE_CURSOR_ARROW      0
        #define MOUSE_CURSOR_MOVE       1
        #define MOUSE_CURSOR_INFO       2
        #define MOUSE_CURSOR_OPEN       3
        #define MOUSE_CURSOR_CLOSE      4
        #define MOUSE_CURSOR_TAKE       5
        #define MOUSE_CURSOR_USE        6
        #define MOUSE_CURSOR_TALK       7
        ...
  ----------------------------------------------------- */
int cls_Mouse::fct_GetNextActionCursor()
{

    if (n_CurrentActionCursor==NB_MOUSE_CURSOR_ARROW-1)
    {
        n_CurrentActionCursor=MOUSE_CURSOR_INFO;
    }
    else
    {
        n_CurrentActionCursor++;
    }
    return n_CurrentActionCursor;

}

/* ----------------------------------------------------
    Permet d'obtenir le curseur précédent dans la liste
        #define NB_MOUSE_CURSOR_ARROW   8
        #define MOUSE_CURSOR_ARROW      0
        #define MOUSE_CURSOR_MOVE       1
        #define MOUSE_CURSOR_INFO       2
        #define MOUSE_CURSOR_OPEN       3
        #define MOUSE_CURSOR_CLOSE      4
        #define MOUSE_CURSOR_TAKE       5
        #define MOUSE_CURSOR_USE        6
        #define MOUSE_CURSOR_TALK       7
        ...
  ----------------------------------------------------- */
int cls_Mouse::fct_GetPreviousActionCursor()
{
    if (n_CurrentActionCursor==MOUSE_CURSOR_INFO)
    {
        n_CurrentActionCursor=NB_MOUSE_CURSOR_ARROW-1;
    }
    else
    {
        n_CurrentActionCursor--;
    }
    return n_CurrentActionCursor;
}


/* ----------------------------------------------------
			On Dessine le curseur au bon endroit
  ----------------------------------------------------- */
void cls_Mouse::fct_DrawMouse(SDL_Surface *p_sdlS_Ecran)
{

    SDL_Rect rect;
    int x,y;

    // -----------------------------------------------------------------

    // On recupere la position de la souris
    SDL_GetMouseState(&x,&y);

    //On met a jour la structure rect
    rect.x = x;
    rect.y = y;

    //On met notre image de souris
    SDL_BlitSurface(this->p_sdlS_CurrentMouse, NULL, p_sdlS_Ecran, &rect);

}


/* ----------------------------------------------------
			On libere l'image du curseur
  ----------------------------------------------------- */
void cls_Mouse::fct_FreeSurface()
{

    int i;

    // On libère les surfaces des modèles de souris
    for(i=0;i<NB_MOUSE_CURSOR_ARROW;i++)
    {
        SDL_FreeSurface(this->p_sdlS_Mouse[i]);
    }

    // Pas besoin !!!!
    // -----------------------------------------------------------------
    // On libère la surface de la souris en cours
    //SDL_FreeSurface(this->p_sdlS_CurrentMouse);

}
